package com.wtf.dsp.admin.controller;


import com.wtf.dsp.admin.model.dto.*;
import com.wtf.dsp.admin.model.vo.NsListVO;
import com.wtf.dsp.admin.service.NamespaceService;
import com.wtf.dsp.core.api.R;
import com.wtf.dsp.core.util.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: NamespaceController
 * Description:
 * date: 2023/7/7 16:44
 *
 * @author linhuanyou
 * @version 1.0
 */
@Api(tags = "命名空间")
@RestController
@RequestMapping("/ns")
public class NamespaceController {

    @Autowired
    private NamespaceService namespaceService;

    @ApiOperation(value = "获取命名空间(分页)")
    @GetMapping("/page")
    public R<PageUtils> page(@Validated NsPageDTO nsPageDTO){
        PageUtils pageUtils = namespaceService.page(nsPageDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获取命名空间")
    @GetMapping("/info/{ns}")
    public R<NsListVO> info(@PathVariable String ns){
        NsListVO nsListVO = namespaceService.info(ns);
        return R.ok(nsListVO);
    }

    @ApiOperation(value = "获取命名空间")
    @GetMapping("/list")
    public R list(@Validated NsListDTO nsListDTO){
        List<String> nsListVOList = namespaceService.list(nsListDTO).stream().map(NsListVO::getNs).collect(Collectors.toList());
        return R.ok(nsListVOList);
    }

    @ApiOperation(value = "新增命名空间")
    @PostMapping("/add")
    public R add(@RequestBody @Validated NsAddDTO nsAddDTO){
        int save = namespaceService.add(nsAddDTO);
        return R.ok(save);
    }

    @ApiOperation(value = "删除命名空间")
    @PostMapping("/del")
    public R remove(@RequestBody @Validated NsDelDTO nsDelDTO){
        int del = namespaceService.remove(nsDelDTO);
        return R.ok(del);
    }

    @ApiOperation(value = "编辑命名空间")
    @PostMapping("/update")
    public R update(@RequestBody @Validated NsUpdateDTO nsUpdateDTO){
        int update = namespaceService.update(nsUpdateDTO);
        return R.ok(update);
    }
}
